package com.examples.listeners;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class WidgetsWithListeners {

	private Text firstNameText;
	private Text lastNameText;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		shell.setText("Outer Shell");
		DisposeListener disposeListener = new DispoListener();
		
		WidgetsWithListeners practice = new WidgetsWithListeners();
		practice.createDialogArea(shell).open();
		
		
		Shell shell1 = new Shell(shell.getDisplay());
		shell1.setText("Inner Shell");
		shell1.open();
		shell.addDisposeListener(disposeListener);
		while (!shell.isDisposed()) {
			/* Checking if OS has any other event in queue, If not it goes inside if */
			if (!display.readAndDispatch()) {
				/* Asking the display to remain visible */
				display.sleep();
			}
		}		
	}

	/* Passed an empty shell and returned the same shell with 2 text fields and buttons */
	public Shell createDialogArea(Shell parent) {
		
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;

		parent.setLayout(layout);

		FocusListener listener = new TextFieldFocusListener();

		GridData gridData = new GridData();
		
		Label label1 = new Label(parent, SWT.NONE);
		label1.setText("First Name");
		firstNameText = new Text(parent, SWT.BORDER);
		firstNameText.setLayoutData(gridData);
		firstNameText.addFocusListener(listener);

		gridData = new GridData();

		Label label2 = new Label(parent, SWT.NONE);
		label2.setText("Last Name");
		lastNameText = new Text(parent, SWT.BORDER);
		lastNameText.setLayoutData(gridData);

		MouseListener mouseListener = new MouseHoverListener();
		
		MouseMoveListener mouseMoveListener = new Mmove();
		label2.addMouseMoveListener(mouseMoveListener);
		
		
		Button button = new Button(parent, SWT.NONE);
		button.setText("B1");		
		button.addMouseListener(mouseListener);
		
		return parent;
	}

}

class TextFieldFocusListener implements FocusListener {

	@Override
	public void focusGained(FocusEvent e) {
		System.out.println("focus in");

	}

	@Override
	public void focusLost(FocusEvent e) {
		System.out.println("focus out");

	}

}

class MouseHoverListener implements MouseListener{

	@Override
	public void mouseDoubleClick(MouseEvent e) {
		System.out.println("Mouse Double Clicked");
	}

	@Override
	public void mouseDown(MouseEvent e) {
		System.out.println("Mouse Clicked");
	}

	@Override
	public void mouseUp(MouseEvent e) {
		System.out.println("Mouse Released");
	}
}

class Mmove implements MouseMoveListener{

	@Override
	public void mouseMove(MouseEvent e) {
		System.out.println("Mouse is moving");
		
	}
	
}

class DispoListener implements DisposeListener{

	@Override
	public void widgetDisposed(DisposeEvent e) {
		System.out.println("Widget is disposed");
		
	}
	
}


